library(R.filesets)
## Loading required package: R.oo
## Loading required package: R.methodsS3
## R.methodsS3 v1.8.2 (2022-06-13 22:00:14 UTC) successfully loaded. See ?R.methodsS3 for help.
## R.oo v1.25.0 (2022-06-12 02:20:02 UTC) successfully loaded. See ?R.oo for help.
## 
## Attaching package: 'R.oo'
## The following object is masked from 'package:R.methodsS3':
## 
##     throw
## The following objects are masked from 'package:methods':
## 
##     getClasses, getMethods
## The following objects are masked from 'package:base':
## 
##     attach, detach, load, save
## R.filesets v2.15.0 (2022-07-21 10:00:05 UTC) successfully loaded. See ?R.filesets for help.
## 
## Attaching package: 'R.filesets'
## The following objects are masked from 'package:base':
## 
##     append, readLines
library(rrvgo)
## 
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
GO.info = read.csv("/Users/johncsantiago/Documents/GOcat_info.csv", row.names = 1)

GO.sim = function(GO.file){
  GO.data = GO.file[GO.file[,"p.adjust"]<.05,]
  GO.ontology = "BP"
  GO.use = row.names(GO.info)[GO.info$ontology == GO.ontology]
  GO.data = GO.data[intersect(row.names(GO.data), GO.use),]

  go_analysis = row.names(GO.data)

  scores = setNames(-log10(GO.data[,"p.adjust"]), go_analysis)

  simMatrix <- calculateSimMatrix(go_analysis,
                                orgdb="org.Dm.eg.db",
                                ont=GO.ontology,
                                method="Rel")
  return(simMatrix)
}
  
GO.rt = function(GO.file){
  GO.data = GO.file[GO.file[,"p.adjust"]<.05,]
  GO.ontology = "BP"
  GO.use = row.names(GO.info)[GO.info$ontology == GO.ontology]
  GO.data = GO.data[intersect(row.names(GO.data), GO.use),]

  go_analysis = row.names(GO.data)

  scores = setNames(-log10(GO.data[,"p.adjust"]), go_analysis)
  
  go_analysis = row.names(GO.data)

  reducedTerms <- reduceSimMatrix(simMatrix,
                                scores,
                                threshold=0.7,
                                orgdb="org.Dm.eg.db")

  return(reducedTerms)

}

##Head

GO.file = loadRDS("/Users/johncsantiago/Downloads/GO_Head_df")

simMatrix = GO.sim(GO.file)
## Loading required package: DOSE
## DOSE v3.24.2  For help: https://yulab-smu.top/biomedical-knowledge-mining-book/
## 
## If you use DOSE in published research, please cite:
## Guangchuang Yu, Li-Gen Wang, Guang-Rong Yan, Qing-Yu He. DOSE: an R/Bioconductor package for Disease Ontology Semantic and Enrichment analysis. Bioinformatics 2015, 31(4):608-609
## 
## preparing gene to GO mapping data...
## preparing IC data...
reducedTerms = GO.rt(GO.file)

##Thorax

GO.file = loadRDS("/Users/johncsantiago/Downloads/GO_Thorax_df")

simMatrix = GO.sim(GO.file)
## preparing gene to GO mapping data...
## preparing IC data...
reducedTerms = GO.rt(GO.file)

##Abdomen

GO.file = loadRDS("/Users/johncsantiago/Downloads/GO_Abdomen_df")

simMatrix = GO.sim(GO.file)
## preparing gene to GO mapping data...
## preparing IC data...
reducedTerms = GO.rt(GO.file)